package com.example.test.controller;

import com.example.test.common.BaseResponse;
import com.example.test.common.ServiceExceptionEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * 基础控制器类
 */
public class BaseController {

    protected transient final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 成功返回字符串
     */
    protected static final String SUCCESS = "success";

    /**
     * 返回成功
     * @return
     */
    protected BaseResponse createSuccess() {
        return BaseResponse.success(SUCCESS);
    }

    /**
     * 返回成功
     * @param data
     * @return
     */
    protected BaseResponse createSuccess(Object data) {
        return BaseResponse.success(data);
    }

    /**
     * 返回失败
     * @param msg
     * @see #createError()
     * @return
     */
    @Deprecated
    protected BaseResponse createError(String msg) {
        return createError();
    }

    /**
     * 服务异常
     * @return
     */
    protected BaseResponse createError() {
        return BaseResponse.error(ServiceExceptionEnum.SYS_ERROR);
    }

  /**
   * 服务异常
   * @return
   */
  protected BaseResponse createError(ServiceExceptionEnum serviceExceptionEnum) {
    return BaseResponse.error(serviceExceptionEnum);
  }

    /**
     *
     * @param name
     * @return
     */
    protected Object getReqAttr(String name) {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        return requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }


}
